package com.sunchangming.myweb;

import java.io.IOException;
import java.util.Arrays;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
import com.google.api.client.auth.oauth2.CredentialStore;
import com.google.api.client.extensions.servlet.auth.oauth2.AbstractAuthorizationCodeServlet;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.thoughtworks.xstream.XStream;

public class Auth extends AbstractAuthorizationCodeServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3825069997214236790L;
	
	 static final String TOKEN_SERVER_URL = "https://login.live.com/oauth20_token.srf";
	 static final String AUTHORIZATION_SERVER_URL = "https://login.live.com/oauth20_authorize.srf";
	static final String[] SCOPE = { "wl.signin", "wl.basic", "wl.skydrive",
			"wl.skydrive_update", "wl.offline_access", "wl.contacts_photos",
			"wl.contacts_skydrive" };
	static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
	/** Global instance of the JSON factory. */
	static final JsonFactory JSON_FACTORY = new JacksonFactory();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		response.setStatus(HttpServletResponse.SC_OK);
		response.getWriter().println(
				"<h1>" + this.getCredential().getAccessToken() + "</h1>");
	}

	static MySQLConfig getPersistenceManagerFactory() {
		XStream xstream=new XStream();		
		return (MySQLConfig)xstream.fromXML(new java.io.File("mysqlconfig.xml"));
	}

	static AuthorizationCodeFlow getAuthFlow(){
		OAuth2ClientCredentials cred=OAuth2ClientCredentials.getInstance();
		AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder(
				BearerToken.authorizationHeaderAccessMethod(), Auth.HTTP_TRANSPORT,
				Auth.JSON_FACTORY, new GenericUrl(Auth.TOKEN_SERVER_URL),
				new ClientParametersAuthentication(
						cred.CLIENT_ID,
						cred.CLIENT_SECRET),
						cred.CLIENT_ID, Auth.AUTHORIZATION_SERVER_URL)
				.setScopes(Auth.SCOPE)
				.setCredentialStore(
						(CredentialStore) MySQLCredentialStore.getInstance()).build();
		return codeFlow;
	}
	@Override
	protected AuthorizationCodeFlow initializeFlow() throws ServletException,
			IOException {
		return getAuthFlow();
	}

	@Override
	protected String getRedirectUri(HttpServletRequest req)
			throws ServletException, IOException {
		GenericUrl url = new GenericUrl(req.getRequestURL().toString());		
		url.setRawPath("/oauth2callback.do");
		url.put("userid", this.getUserId(req));
		return url.build();
	}

	@Override
	protected String getUserId(HttpServletRequest req) throws ServletException,
			IOException {
		return req.getParameter("userid");
	}

}
